﻿namespace Launcher.Model

import System
import System.Collections
import System.Drawing
import System.IO

import Boo.Lang.Useful.Attributes

import ShellLink

callable ApplicationEventHandler(app as ApplicationInfo)

class ApplicationInfo(IComparable):
"""Description of ApplicationInfo"""

	event RankingChanged as ApplicationEventHandler
	event PreExecution as ApplicationEventHandler
	event PosExecution as ApplicationEventHandler
	
	[getter(Name)]
	_name as string

	[getter(Description)]
	_description as string
	
	[getter(Icon)]
	_icon as Icon
	
	[getter(Path)]
	_path as string
	
	[getter(Arguments)]
	_arguments as string
	
	[getter(Ranking)]
	_ranking as int
	
	def constructor([required] path as string):
		_name = IO.Path.GetFileNameWithoutExtension(path)
		_icon = Icon.ExtractAssociatedIcon(path)
		
		GetShortcutInfo(path)
		GetRanking(_path)

	private def GetShortcutInfo(path as string):
		sl = ShellShortcut(path)
		_description = sl.Description
		_path = sl.Path
		_arguments = sl.Arguments
	
	private def GetRanking(path as string):
		RankingFile.Instance.OptimizedLoad("ranking.txt")
		_ranking = RankingFile.Instance.AppRanking[GetRankingKey()]

	private def GetRankingKey():
		return "${_path}_${_arguments}"

	def IsValid():
		return File.Exists(_path) and _path.EndsWith("exe")
		
	def CompareTo(other as object) as int:
		l as ApplicationInfo = self
		r as ApplicationInfo = other
		
		ret = l.Ranking.CompareTo(r.Ranking)
		if ret != 0: return -ret
		return l.Name.CompareTo(r.Name)

	def Execute():
		PreExecution(self)
		IncrementRanking()
		shellp(_path, _arguments)
		PosExecution(self)
		
	private def IncrementRanking():
		_ranking++
		RankingFile.Instance.AppRanking[GetRankingKey()] = _ranking
		RankingChanged(self)
		
	def Matches(match as string):
		path = IO.Path.GetFileNameWithoutExtension(Path)
		return true if Name.Contains(match)
		return true if Description.Contains(match)
		return path.Contains(match)
